#include "Panels.h"

#include "data\slide_show.hpp"
#include "Communicate.h"
#include <resource_cache\resource_cache.hpp>
#include "string_conv.h"
#include "UI\interface_ids.hpp"


void LeftPanel::clear_slide_ui()
{
	g_slides.current_slide = -1;
	m_slides_interface.m_slides->Clear();
	m_slides_interface.m_background.set_text("");
	m_slides_interface.m_gravX.set_text("");
	m_slides_interface.m_gravY.set_text("");
	m_slides_interface.m_name.set_text("");
}

void LeftPanel::set_slide_ui()
{
	clear_slide_ui();
	for( unsigned int i = 0; i < g_slides.slides.size(); ++i )
	{
		m_slides_interface.m_slides->Append( wxT("bob") );
		m_slides_interface.m_slides->SetString( i, g_slides.slides[i].name.c_str() );
	}
}

void LeftPanel::on_new_slide(wxCommandEvent & WXUNUSED(e))
{
	slide new_slide;
	g_slides.slides.push_back( new_slide );
	m_slides_interface.m_slides->Append( wxT("New Slide") );
}


void LeftPanel::on_slide_rename(wxCommandEvent & e)
{
	int cslide = g_slides.current_slide;
	if( cslide < 0 || cslide >= (int)g_slides.slides.size() )
		return;

	m_slides_interface.m_slides->SetString( cslide, e.GetString() );
	g_slides.slides[cslide].name = e.GetString();
}

void LeftPanel::on_slide_select( wxCommandEvent & WXUNUSED(e) )
{
	int selection = m_slides_interface.m_slides->GetSelection();

	if( selection < 0 || selection >= (int)g_slides.slides.size() )
		return;

	g_slides.current_slide = selection;

	slide & active_slide = g_slides.slides[g_slides.current_slide];

	m_slides_interface.m_name.set_text( active_slide.name );
	m_slides_interface.m_background.set_text( active_slide.background_file );
	m_slides_interface.m_gravX.set_text( stringify(active_slide.gravity.x) );
	m_slides_interface.m_gravY.set_text( stringify(active_slide.gravity.y) );

	//clear_body_ui();
	//set_to_current_body_ui();
	m_slide_data.m_body_ui.m_bodys->Clear();
	for( unsigned int i=0; i<g_slides.slides[g_slides.current_slide].bodys.size(); ++i )
	{
		m_slide_data.m_body_ui.m_bodys->Append( g_slides.slides[g_slides.current_slide].bodys[i].name );
	}
}

void LeftPanel::on_delete_slide(wxCommandEvent & WXUNUSED(e))
{
	//TODO add are you sure
	int cslide = g_slides.current_slide;
	if( cslide < 0 || cslide >= (int)g_slides.slides.size() )
		return;

	m_slides_interface.m_slides->Delete( cslide );
	g_slides.slides.erase(g_slides.slides.begin() + cslide );
}
void LeftPanel::on_browse_background(wxCommandEvent & WXUNUSED(e))
{
	int cslide = g_slides.current_slide;
	if( cslide < 0 || cslide >= (int)g_slides.slides.size() )
		return;

	wxFileDialog * openFileDialog = new wxFileDialog(this);

	wxString file_name;
	if (openFileDialog->ShowModal() == wxID_OK)
	{
		file_name = openFileDialog->GetPath();  
	}

	//Communicate *comm = (Communicate *) m_parent->GetParent();
	//comm->m_rp->m_canvas->mySprite.SetImage( * g_cache.load_image( (const char*)(fileName.mb_str(wxConvUTF8)) ) );

	slide & active_slide = g_slides.slides[g_slides.current_slide];

	std::string f = file_name;
	active_slide.set_background( f );
	m_slides_interface.m_background.set_text( f );

	delete openFileDialog;
}
void LeftPanel::on_background_rename(wxCommandEvent & e)
{
	int cslide = g_slides.current_slide;
	if( cslide < 0 || cslide >= (int)g_slides.slides.size() )
		return;

	g_slides.slides[g_slides.current_slide].background_file = e.GetString();
}
void LeftPanel::on_gravity_x(wxCommandEvent & e)
{
	int cslide = g_slides.current_slide;
	if( cslide < 0 || cslide >= (int)g_slides.slides.size() )
		return;

	g_slides.slides[g_slides.current_slide].gravity.x = ::atof(e.GetString().char_str());
}
void LeftPanel::on_gravity_y(wxCommandEvent & e)
{
	int cslide = g_slides.current_slide;
	if( cslide < 0 || cslide >= (int)g_slides.slides.size() )
		return;

	g_slides.slides[g_slides.current_slide].gravity.y = ::atof(e.GetString().char_str());
}
